<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/11/6
 * Time: 5:42 PM
 */

namespace App\VO\Usercenter\UserCoupon;

use App\Entity\CouponCodeEntity;
use App\Entity\ProductNewEntity;
use App\Entity\UserCouponEntity;
use App\Models\UserCoupon;

class IndexUserCouponModelVO
{
    /**
     * @var UserCoupon
     */
    private $model;

    /**
     * @var CouponCodeEntity
     */
    private $couponCodeEntity;

    /**
     * @var UserCouponEntity
     */
    private $userCouponEntity;

    public function __construct(UserCoupon $model)
    {
        $this->model = $model;
        $this->userCouponEntity = new UserCouponEntity($model);
        $this->couponCodeEntity = new CouponCodeEntity($model->coupon);
    }

    /**
     * @return CouponCodeEntity
     */
    public function getCouponCodeEntity()
    {
        return $this->couponCodeEntity;
    }

    /**
     * @return UserCouponEntity
     */
    public function getUserCouponEntity()
    {
        return $this->userCouponEntity;
    }

    /**
     * 优惠券可用时间的描述
     */
    public function getCouponUseAblePeriodDes()
    {
        $couponCodeEntity = $this->couponCodeEntity;

        $str = explode(' ', $couponCodeEntity->getNotBefore())[0];

        if ($couponCodeEntity->getNotAfter()) {
            $str .= '至' . explode(' ', $couponCodeEntity->getNotAfter())[0];
        } else {
            $str .= '起';
        }
        return $str;
    }

    /**
     * 获取跳转连接
     */
    public function getToUrl()
    {
        $couponCodeEntity = $this->couponCodeEntity;
        $btype = $couponCodeEntity->getBtype();

        if (!$btype) {
            return route('products');
        }

        $productId = ProductNewEntity::getProductIdByBtype($btype);
        if ($productId) {
            $url = route('productsShow', ['product' => $productId]);
        } else {
            $url = route('products');
        }
        return $url;
    }
}
